package cn.jimmie.learn.viewArt.weight

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.*
import android.os.Build
import android.support.annotation.RequiresApi
import android.util.AttributeSet
import android.view.animation.LinearInterpolator

/**
 * function :
 *
 * <p> Create Time: 2019年01月20日 13:08 </p>
 * <p> @author Jimmie.Qian  </p>
 */

open class ButterflyView(context: Context, attrs: AttributeSet) : BaseView(context, attrs) {
    private val mPath = Path()
    private val mCamera = Camera()
    private val mMatrix = Matrix()
    private var mActionDegree = 0f

    private val mFlyAnimator = ValueAnimator.ofInt(150_000)

    // 是否是正方向的
    private var isPositive = true
    private var mLastAnimatedValue = 0
    // 蝴蝶扇动的速度
    var mFlySpeed = 4f
        set(value) {
            field = 4 * value
        }

    init {
        paint.strokeCap = Paint.Cap.ROUND
        paint.color = Color.GREEN

        mFlyAnimator.interpolator = LinearInterpolator()
        mFlyAnimator.duration = (15000_000 / mFlySpeed).toLong()
        mFlyAnimator.start()

        mFlyAnimator.addUpdateListener {
            val value = it.animatedValue as Int % 15
            if (value < mLastAnimatedValue) isPositive = !isPositive
            mLastAnimatedValue = value

            mActionDegree = if (isPositive) 35 + value * 1f else 50 - value * 1f
            invalidate()
        }
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onDraw(canvas: Canvas) {
        drawButterfly(canvas)
    }

    override fun onDetachedFromWindow() {
        mFlyAnimator.cancel()
        mFlyAnimator.removeAllUpdateListeners()
        super.onDetachedFromWindow()
    }

    private fun drawButterflyLeft(canvas: Canvas) {
        mPath.reset()
        mPath.moveTo(w / 2f, h / 2f)
        mPath.cubicTo(
            w / 5f, -0.5f * h,
            0f, 0.65f * h,
            w / 2f, h / 2f
        )
        mPath.cubicTo(
            w / 5f, 0.4f * h,
            w / 5f, 1.2f * h,
            w / 2f, h / 2f
        )
        paint.color = Color.parseColor("#ff8000")
        canvas.drawPath(mPath, paint)
        paint.color = Color.YELLOW
        canvas.drawCircle(0.3f * w, 0.28f * h, w / 20f, paint)
        canvas.drawCircle(0.33f * w, 0.7f * h, w / 30f, paint)
    }

    private fun drawButterflyRight(canvas: Canvas) {
        mPath.reset()
        mPath.moveTo(w / 2f, h / 2f)
        mPath.cubicTo(
            w * 4f / 5, -0.5f * h,
            w * 1f, 0.65f * h,
            w / 2f, h / 2f
        )
        mPath.cubicTo(
            w * 4f / 5, 0.4f * h,
            w * 4f / 5, 1.2f * h,
            w / 2f, h / 2f
        )
        paint.color = Color.parseColor("#ff8000")
        canvas.drawPath(mPath, paint)
        paint.color = Color.YELLOW
        canvas.drawCircle(0.7f * w, 0.28f * h, w / 20f, paint)
        canvas.drawCircle(0.67f * w, 0.7f * h, w / 30f, paint)
    }

    private fun cameraRotate(canvas: Canvas, degree: Float) {
        mCamera.save()
        if (mActionDegree > 0) mCamera.setLocation(0f, 0f, 8f)
        mCamera.rotate(0f, degree, 0f)
        mCamera.getMatrix(mMatrix)

        mMatrix.preTranslate(-w / 2f, -h / 2f)
        mMatrix.postTranslate(w / 2f, h / 2f)

        canvas.concat(mMatrix)
        mCamera.restore()
    }

    private fun drawButterfly(canvas: Canvas) {
        // 身子
        paint.style = Paint.Style.FILL
        paint.color = Color.parseColor("#f5de83")
        paint.strokeWidth = 10 * dp
        canvas.drawLine(w / 2f, h * 2 / 5f, w / 2f, h * 5 / 8f, paint)

        // 触角
        paint.color = Color.BLACK
        paint.strokeWidth = 2 * dp
        paint.style = Paint.Style.STROKE
        mPath.reset()
        mPath.moveTo(w / 2f - 2 * dp, h * 0.4f)
        mPath.cubicTo(
            w / 2f - 2 * dp, h * 0.2f,
            w * 0.4f, h * 0.2f,
            w * 0.45f, h * 0.25f
        )
        mPath.moveTo(w / 2f + 2 * dp, h * 0.4f)
        mPath.cubicTo(
            w / 2f + 2 * dp, h * 0.2f,
            w * 0.6f, h * 0.2f,
            w * 0.55f, h * 0.25f
        )
        canvas.drawPath(mPath, paint)

        // 触角的两个点
        paint.strokeWidth = 5 * dp
        canvas.drawPoint(w * 0.45f, h * 0.25f, paint)
        canvas.drawPoint(w * 0.55f, h * 0.25f, paint)

        // 左边蝴蝶
        paint.style = Paint.Style.FILL
        canvas.save()
        canvas.clipRect(0, 0, w / 2, h)
        cameraRotate(canvas, Math.abs(mActionDegree))
        drawButterflyLeft(canvas)
        canvas.restore()

        // 右边蝴蝶
        canvas.clipRect(w / 2, 0, w, h)
        cameraRotate(canvas, -Math.abs(mActionDegree))
        drawButterflyRight(canvas)
    }
}